#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: gtk_modes.py
Date: April 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manages the modes pop-up window

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep
import subprocess

# Import GTK libraries
import pygtk
import gtk
import gtk.glade
import gobject

# Application imports
import utils

#======================================================================
# The Filters pop-up.
class ModesGui:
  
    #---------------------------------------------------------------------
    def __init__(self, modes_tree, caps, dsp_pub, persist_service, callback):
        self.modes_tree = modes_tree
        self.caps = caps
        self.dsp_pub = dsp_pub
        self.persist_service = persist_service
        self.callback = callback
        self.ignore = False
        
        #-----------------------------------------------------------------------
        # Wire up the events
        dic =   {
            # Modes
            "on_tb_lsb_toggled" : self.mode_lsb,
            "on_tb_usb_toggled" : self.mode_usb,
            "on_tb_dsb_toggled" : self.mode_dsb,
            "on_tb_cwl_toggled" : self.mode_cwl,
            "on_tb_cwu_toggled" : self.mode_cwu,
            "on_tb_fmn_toggled" : self.mode_fmn,
            "on_tb_am_toggled" : self.mode_am,
            "on_tb_sam_toggled" : self.mode_sam,
            "on_tb_digil_toggled" : self.mode_digil,
            "on_tb_digiu_toggled" : self.mode_digiu,
            "on_tb_spec_toggled" : self.mode_spec,
            
            "on_b_cancel_mode_clicked" : self.cancel,
        }
        
        # This will connect up all the events in one hit
        self.modes_tree.signal_autoconnect (dic)
    
        # Now we need a reference to all the controls
        self.blsb = self.modes_tree.get_widget('tb_lsb')
        self.busb = self.modes_tree.get_widget('tb_usb')
        self.bdsb = self.modes_tree.get_widget('tb_dsb')
        self.bcwl = self.modes_tree.get_widget('tb_cwl')
        self.bcwu = self.modes_tree.get_widget('tb_cwu')
        self.bfmn = self.modes_tree.get_widget('tb_fmn')
        self.bam = self.modes_tree.get_widget('tb_am')
        self.bsam = self.modes_tree.get_widget('tb_sam')
        self.bdigil = self.modes_tree.get_widget('tb_digil')
        self.bdigiu = self.modes_tree.get_widget('tb_digiu')
        self.bspec = self.modes_tree.get_widget('tb_spec')
        
        # Avoid circular events
        self.ignore = False
        
        self.w=self.modes_tree.get_widget('Modes')
        self.w.set_position(gtk.WIN_POS_MOUSE)

        # Widget map
        self.dict = {'LSB':0,'USB':1,'DSB':2,'CW-L':3,'CW-U':4,'FM-N':5,'AM':6,'SAM':7,'DIGI-L':8,'DIGI-U':9,'SPEC':10}
        self.widgets = [self.blsb, self.busb, self.bdsb, self.bcwl, self.bcwu, self.bfmn, self.bam, self.bsam, self.bdigil, self.bdigiu, self.bspec]        
        
        # Adjust for capabilities
        for widget in self.widgets:
            widget.set_sensitive(False)
        allowed_modes = self.caps.rsplit(',')
        for allowed_mode in allowed_modes:
            if allowed_mode in self.dict:
                self.widgets[self.dict[allowed_mode]].set_sensitive(True) 
    
    #--------------------------------------------------------------------- 
    # Public Interface
    #---------------------------------------------------------------------
    def show_window(self):
        self.w.show_all()
    
    #---------------------------------------------------------------------
    def hide_window(self):
        self.w.hide()
        
    #---------------------------------------------------------------------
    # GUI generated events (i.e. things that happen when you play with widgets)
    # Events for the mode buttons
    #---------------------------------------------------------------------  
    #---------------------------------------------------------------------  
    def mode_lsb(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('LSB')
            self.updater('LSB')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def mode_usb(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('USB')
            self.updater('USB')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def mode_dsb(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('DSB')
            self.updater('DSB')
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def mode_cwl(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('CW-L')
            self.updater('CW-L')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def mode_cwu(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('CW-U')
            self.updater('CW-U')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def mode_fmn(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('FM-N')
            self.updater('FM-N')
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def mode_am(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('AM')
            self.updater('AM')
            self.w.hide()
                
    #---------------------------------------------------------------------  
    def mode_sam(self, widget):
        if not self.ignore:
            self.dsp_pub.setMode('SAM')
            self.updater('SAM')
            self.w.hide()

    #---------------------------------------------------------------------  
    def mode_digil(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('DIGI-L')
            self.updater('DIGI-L')
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def mode_digiu(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('DIGI-U')
            self.updater('DIGI-U')
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def mode_spec(self, widget):
        if not self.ignore:
            #self.dsp_pub.setMode('SPEC')
            self.updater('SPEC')
            self.w.hide()
    
    #--------------------------------------------------------------------- 
    def updater(self, value):
        self.callback(value)
            
    #---------------------------------------------------------------------  
    def cancel(self, widget):
        self.w.hide()
        
    #---------------------------------------------------------------------  
    # Update events
    #---------------------------------------------------------------------  
    def mode_update(self, data):
        self.set_mode_hints(data)
    
    #---------------------------------------------------------------------
    # Private
    #---------------------------------------------------------------------
    def set_mode_hints(self, mode):
        self.ignore = True
        for n in range(len(self.widgets)):
            if n == self.dict[mode]:
                self.widgets[n].set_active(True)
            else:
                self.widgets[n].set_active(False)
        self.ignore = False